package com.detection.hikvision.utils;

import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.hikvision.artemis.sdk.ArtemisHttpUtil;
import com.hikvision.artemis.sdk.config.ArtemisConfig;
import com.detection.hikvision.config.CusArtemisConfig;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

/**
 * @author LH
 * @date 2022/9/9 15:36
 */
public class HaiKangHttpUtils {

    public String send(CusArtemisConfig cusArtemisConfig,JSONObject jsonObject,ArtemisApi api){
        ArtemisConfig.host = cusArtemisConfig.getHost(); // 平台的ip端口
        ArtemisConfig.appKey = cusArtemisConfig.getAppKey();  // 密钥appkey
        ArtemisConfig.appSecret = cusArtemisConfig.getAppSecret();// 密钥appSecret

        /**
         * STEP2：设置OpenAPI接口的上下文
         */
        final String ARTEMIS_PATH = cusArtemisConfig.getArtemisPath();
//        final String previewURLsApi = ARTEMIS_PATH + "/api/irds/v2/region/nodesByParams";
        final String previewURLsApi = ARTEMIS_PATH + api.getPath();
        Map<String, String> path = new HashMap<String, String>(2) {
            {
                put(cusArtemisConfig.getTransferProtocol() + "://", previewURLsApi);//根据现场环境部署确认是http还是https
            }
        };

        /**
         * STEP4：设置参数提交方式
         */
        String contentType = "application/json";

        /**
         * STEP5：组装请求参数
         */
        /**
        JSONObject jsonBody = new JSONObject();
        jsonBody.put("resourceType", "region");
        jsonBody.put("parentIndexCodes", new String[]{"root000000"});
        jsonBody.put("isSubRegion", true);
        jsonBody.put("pageNo", 1);
        jsonBody.put("pageSize", 20);
        jsonBody.put("authCodes", new String[]{"view"});
        jsonBody.put("regionType", 10);
        jsonBody.put("sonOrgIndexCodes", new String[]{"root000000"});
        jsonBody.put("cascadeFlag", 0);
        jsonBody.put("orderBy", "regionName");
        jsonBody.put("orderType", "desc");
        String body = jsonBody.toJSONString();
         */
        /**
         * STEP6：调用接口
         */
        String s = ArtemisHttpUtil.doPostStringArtemis(path, jsonObject.toJSONString(), null, null, contentType, null);
        if (Objects.isNull(s)){
            throw new RuntimeException("无法获取海康平台设备数据");
        }
        return s;
    }

    public String doSendGet(CusArtemisConfig cusArtemisConfig,JSONObject jsonObject,ArtemisApi api){
        ArtemisConfig.host = cusArtemisConfig.getHost(); // 平台的ip端口
        ArtemisConfig.appKey = cusArtemisConfig.getAppKey();  // 密钥appkey
        ArtemisConfig.appSecret = cusArtemisConfig.getAppSecret();// 密钥appSecret

        /**
         * STEP2：设置OpenAPI接口的上下文
         */
        final String ARTEMIS_PATH = cusArtemisConfig.getArtemisPath();
//        final String previewURLsApi = ARTEMIS_PATH + "/api/irds/v2/region/nodesByParams";
        final String previewURLsApi = ARTEMIS_PATH + api.getPath();
        Map<String, String> path = new HashMap<String, String>(2) {
            {
                put(cusArtemisConfig.getTransferProtocol() + "://", previewURLsApi);//根据现场环境部署确认是http还是https
            }
        };

        /**
         * STEP4：设置参数提交方式
         */
        String contentType = "application/json";

        /**
         * STEP5：组装请求参数  Map
         */
        Map<String, String> querys = new HashMap<>();
        for (Map.Entry<String, Object> entry :jsonObject.entrySet()){
            querys.put(entry.getKey(),entry.getValue().toString());
        }
        /**
         * STEP6：调用接口
         */
        String s = ArtemisHttpUtil.doGetArtemis(path, querys, null, contentType, null);
        if (Objects.isNull(s)){
            throw new RuntimeException("无法获取海康平台设备数据");
        }
        return s;
    }

    public String sendHead(CusArtemisConfig cusArtemisConfig,JSONObject jsonObject,ArtemisApi api){
        ArtemisConfig.host = cusArtemisConfig.getHost(); // 平台的ip端口
        ArtemisConfig.appKey = cusArtemisConfig.getAppKey();  // 密钥appkey
        ArtemisConfig.appSecret = cusArtemisConfig.getAppSecret();// 密钥appSecret

        /**
         * STEP2：设置OpenAPI接口的上下文
         */
        final String ARTEMIS_PATH = cusArtemisConfig.getArtemisPath();
//        final String previewURLsApi = ARTEMIS_PATH + "/api/irds/v2/region/nodesByParams";
        final String previewURLsApi = ARTEMIS_PATH + api.getPath();
        Map<String, String> path = new HashMap<String, String>(2) {
            {
                put(cusArtemisConfig.getTransferProtocol() + "://", previewURLsApi);//根据现场环境部署确认是http还是https
            }
        };

        /**
         * STEP4：设置参数提交方式
         */
        String contentType = "application/json";
        HashMap<String , String> header = new HashMap<>();
        header.put("tagId","frs");

        /**
         * STEP5：组装请求参数
         */
        /**
         JSONObject jsonBody = new JSONObject();
         jsonBody.put("resourceType", "region");
         jsonBody.put("parentIndexCodes", new String[]{"root000000"});
         jsonBody.put("isSubRegion", true);
         jsonBody.put("pageNo", 1);
         jsonBody.put("pageSize", 20);
         jsonBody.put("authCodes", new String[]{"view"});
         jsonBody.put("regionType", 10);
         jsonBody.put("sonOrgIndexCodes", new String[]{"root000000"});
         jsonBody.put("cascadeFlag", 0);
         jsonBody.put("orderBy", "regionName");
         jsonBody.put("orderType", "desc");
         String body = jsonBody.toJSONString();
         */
        /**
         * STEP6：调用接口
         */
        String s = ArtemisHttpUtil.doPostStringArtemis(path, jsonObject.toJSONString(), null, null, contentType, header);
        if (Objects.isNull(s)){
            throw new RuntimeException("无法获取海康平台设备数据");
        }
        return s;
    }

    public String sendArray(CusArtemisConfig cusArtemisConfig,JSONArray jsonArray,ArtemisApi api){
        ArtemisConfig.host = cusArtemisConfig.getHost(); // 平台的ip端口
        ArtemisConfig.appKey = cusArtemisConfig.getAppKey();  // 密钥appkey
        ArtemisConfig.appSecret = cusArtemisConfig.getAppSecret();// 密钥appSecret

        /**
         * STEP2：设置OpenAPI接口的上下文
         */
        final String ARTEMIS_PATH = cusArtemisConfig.getArtemisPath();
//        final String previewURLsApi = ARTEMIS_PATH + "/api/irds/v2/region/nodesByParams";
        final String previewURLsApi = ARTEMIS_PATH + api.getPath();
        Map<String, String> path = new HashMap<String, String>(2) {
            {
                put(cusArtemisConfig.getTransferProtocol() + "://", previewURLsApi);//根据现场环境部署确认是http还是https
            }
        };

        /**
         * STEP4：设置参数提交方式
         */
        String contentType = "application/json";

        /**
         * STEP5：组装请求参数
         */
        /**
         JSONObject jsonBody = new JSONObject();
         jsonBody.put("resourceType", "region");
         jsonBody.put("parentIndexCodes", new String[]{"root000000"});
         jsonBody.put("isSubRegion", true);
         jsonBody.put("pageNo", 1);
         jsonBody.put("pageSize", 20);
         jsonBody.put("authCodes", new String[]{"view"});
         jsonBody.put("regionType", 10);
         jsonBody.put("sonOrgIndexCodes", new String[]{"root000000"});
         jsonBody.put("cascadeFlag", 0);
         jsonBody.put("orderBy", "regionName");
         jsonBody.put("orderType", "desc");
         String body = jsonBody.toJSONString();
         */
        /**
         * STEP6：调用接口
         */
        String s = ArtemisHttpUtil.doPostStringArtemis(path, jsonArray.toJSONString(), null, null, contentType, null);
        if (Objects.isNull(s)){
            throw new RuntimeException("无法获取海康平台设备数据");
        }
        return s;
    }



}
